﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

using MySql.Data.MySqlClient;

namespace MakeIT.MiraPark.Negocios
{
    public static partial class ModelBO
    {

        public static Decimal getCabineSaldo(int idCabine)
        {
            decimal saldo = 0.0M;
            StringBuilder query = new StringBuilder();

            query.AppendLine("SELECT saldo ");
            query.AppendLine("FROM cabinexmovimento ");
            query.AppendLine("WHERE IdCabine=?IdCabine ");
            query.AppendLine("ORDER BY DataMov DESC ");

            MySqlCommand cmd = getConnection();
            cmd.CommandText = query.ToString();

            cmd.Parameters.AddWithValue("?IdCabine", idCabine);

            MySqlDataReader reader = null;
            try
            {
                cmd.Connection.Open();

                reader = cmd.ExecuteReader();

                if (reader.Read())
                {
                    saldo = reader.GetInt32("saldo");
                }
                reader.Close();
                return saldo;
            }
            catch (Exception ex) { throw ex; }
            finally { cmd.Connection.Close(); }
        }

        public static List<CabineXMovimentoVO> getCabineXMovimento(int? idCabine, DateTime? dataMov, int linhas)
        {
            StringBuilder query = new StringBuilder();

            String where = "WHERE ";
            query.AppendLine("SELECT IdCabine, DataMov, Movimento, Credito, Debito, Saldo");
            query.AppendLine("FROM cabinexmovimento ");
            if (idCabine != null)
            {
                query.AppendLine(where + "IdCabine=?IdCabine ");
                where = "AND ";
            }
            if (dataMov != null)
            {
                query.AppendLine(where + "DATE(DataMov)>=DATE(?DataMov) ");
                where = "AND ";
            }
            query.AppendLine("ORDER BY DataMov DESC ");

            MySqlCommand cmd = getConnection();
            cmd.CommandText = query.ToString();

            if (idCabine != null)
                cmd.Parameters.AddWithValue("?IdCabine", idCabine);
            if (dataMov != null)
                cmd.Parameters.AddWithValue("?DataMov", dataMov);

            MySqlDataReader reader = null;
            try
            {
                cmd.Connection.Open();

                reader = cmd.ExecuteReader();

                List<CabineXMovimentoVO> listCabineXMovimentoVOs = new List<CabineXMovimentoVO>();

                while (reader.Read() && linhas-- >0)
                {
                    CabineXMovimentoVO cabinemovimentoVO = CabineXMovimentoVO.CreateCabineXMovimentoVO();
                    cabinemovimentoVO.idCabine = reader.GetInt32("IdCabine");
                    cabinemovimentoVO.dataMov = reader.GetDateTime("DataMov");
                    cabinemovimentoVO.movimento = reader.GetString("Movimento");
                    cabinemovimentoVO.credito = reader.GetDecimal("Credito");
                    cabinemovimentoVO.debito = reader.GetDecimal("Debito");
                    cabinemovimentoVO.saldo = reader.GetDecimal("Saldo");

                    listCabineXMovimentoVOs.Add(cabinemovimentoVO);
                }
                reader.Close();
                return listCabineXMovimentoVOs;
            }
            catch (Exception ex) { throw ex; }
            finally { cmd.Connection.Close(); }
        }
 
        public static long newCabineXMovimento(CabineXMovimentoVO cabineXMovimento)
        {
            StringBuilder query = new StringBuilder();

            query.AppendLine("INSERT INTO cabinexmovimento ");
            query.AppendLine("SET IdCabine=?IdCabine, DataMov=?DataMov, Movimento=?Movimento, Credito=?Credito, Debito=?Debito, Saldo=?Saldo ");

            MySqlCommand cmd = getConnection();
            cmd.CommandText = query.ToString();

            cmd.Parameters.AddWithValue("?IdCabine", cabineXMovimento.idCabine);
            cmd.Parameters.AddWithValue("?DataMov", cabineXMovimento.dataMov);
            cmd.Parameters.AddWithValue("?Movimento", cabineXMovimento.movimento);
            cmd.Parameters.AddWithValue("?Credito", cabineXMovimento.credito);
            cmd.Parameters.AddWithValue("?Debito", cabineXMovimento.debito);
            cmd.Parameters.AddWithValue("?Saldo", cabineXMovimento.saldo);

            try
            {
                cmd.Connection.Open();

                cmd.ExecuteNonQuery();
                return cmd.LastInsertedId;
            }
            catch (Exception ex) { throw ex; }
            finally { cmd.Connection.Close(); }

        }


/*        public static List<AlertaVO> getAlertas()
        {
            return getAlertas(null, null, null);
        }
        public static void setAlerta(AlertaVO cabinemovimentoVO)
        {
            StringBuilder query = new StringBuilder();

            query.AppendLine("UPDATE cabinemovimento ");
            query.AppendLine("SET DataMov=?DataMov, Movimento=?Movimento, Credito=?Credito, Debito=?Debito, Saldo=?Saldo, ApenasComunicar=?ApenasComunicar, Situacao=?Situacao ");
            query.AppendLine("WHERE IdCabine=?IdCabine ");

            MySqlCommand cmd = getConnection();
            cmd.CommandText = query.ToString();

            cmd.Parameters.AddWithValue("?IdCabine", cabinemovimentoVO.idAlerta);
            cmd.Parameters.AddWithValue("?Credito", cabinemovimentoVO.mensagem);
            cmd.Parameters.AddWithValue("?Movimento", cabinemovimentoVO.idCabine);
            cmd.Parameters.AddWithValue("?DataMov", cabinemovimentoVO.dataMov);
            cmd.Parameters.AddWithValue("?Debito", cabinemovimentoVO.reterSaida);
            cmd.Parameters.AddWithValue("?Saldo", cabinemovimentoVO.impedirEntrada);
            cmd.Parameters.AddWithValue("?ApenasComunicar", cabinemovimentoVO.apenasComunicar);
            cmd.Parameters.AddWithValue("?Situacao", cabinemovimentoVO.movimento);

            try
            {
                cmd.Connection.Open();

                cmd.ExecuteNonQuery();
            }
            catch (Exception ex) { throw ex; }
            finally { cmd.Connection.Close(); }

        }


        public static void delAlerta(AlertaVO cabinemovimentoVO)
        {
            StringBuilder query = new StringBuilder();

            query.AppendLine("DELETE FROM cabinemovimento ");
            query.AppendLine("WHERE IdCabine=?IdCabine ");

            MySqlCommand cmd = getConnection();
            cmd.CommandText = query.ToString();

            cmd.Parameters.AddWithValue("?IdCabine", cabinemovimentoVO.idAlerta);

            try
            {
                cmd.Connection.Open();

                cmd.ExecuteNonQuery();
            }
            catch (Exception ex) { throw ex; }
            finally { cmd.Connection.Close(); }

        }
 */
    }
}
